//
//  currentdayVC.m
//  qulvyou
//
//  Created by mmyy on 15/10/22.
//  Copyright (c) 2015年 mmyy. All rights reserved.
//

#import "currentdayVC.h"
#import "CurrentDayItems.h"
#import "currentView.h"
#import "gushixiangqingVC.h"
@interface currentdayVC ()<UITableViewDataSource, UITableViewDelegate>

@end

@implementation currentdayVC
{
    NSInteger start;
    NSMutableArray *_dataSource;
    UITableView *_tableView;
    BOOL more;
    BOOL isUpdata;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    start = 0;
    more = YES;
    isUpdata = YES;
    self.view.backgroundColor = [UIColor purpleColor];
    [self loadLocalData];
    [self loadData];
    // Do any additional setup after loading the view.
}
-(void)loadLocalData
{
    _dataSource = [NSMutableArray arrayWithArray:[CurrentDayItems getAllDataFromCoreData]];
    [self createTableView];

}
-(void)loadData
{
    [Funcation getWithURL:[NSString stringWithFormat:currentdayUrl, (long)start] success:^(id responseObject) {
        if (isUpdata) {
            [CurrentDayItems removeAllData];
            [_dataSource removeAllObjects];
            isUpdata = NO;
        }
        NSDictionary *data = [responseObject objectForKey:@"data"];
        NSArray *hot_spot_list = [data objectForKey:@"hot_spot_list"];
        more = (BOOL)[responseObject objectForKey:@"more"];
        for (NSDictionary *dict in hot_spot_list) {
            CurrentDayItems *items = [CurrentDayItems currentDayItemsWithDict:dict];
            [_dataSource addObject:items];
        }
        start = _dataSource.count;
        [_tableView reloadData];
        [_tableView footerEndRefreshing];
        [_tableView headerEndRefreshing];
    } andFail:^(id error) {
        NSLog(@"falie");
    }];
    
}
-(void)createTableView
{
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 110 - 44) style:UITableViewStylePlain];
    
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.backgroundColor = [UIColor clearColor];
    _tableView.separatorColor = [UIColor clearColor];
    _tableView.rowHeight = 180;
    _tableView.allowsSelection = NO;
    __weak UITableView *tab = _tableView;
    __weak currentdayVC *isSelf = self;
    [_tableView addHeaderWithCallback:^{
        tab.headerRefreshingText = @"下拉刷新";
        isUpdata = YES;
        start = 0;
        [isSelf loadData];
        
    }];
    [_tableView addFooterWithCallback:^{
        tab.footerRefreshingText = @"上拉加载更多";
        isUpdata = NO;
        [isSelf loadData];

        
    }];
    [self.view addSubview:_tableView];
    
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataSource.count / 2;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *ident = @"basis_cell";
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ident];
    }
    CGFloat width = (self.view.frame.size.width - 20) / 2.0;
    for (int i = 0; i < 2; i ++) {
        UIView *leftView = [currentView createViewToFreame:CGRectMake(5 + i * (width + 10), 5, width, 170) withData:[_dataSource objectAtIndex:indexPath.row * 2 + i]];
        leftView.tag = indexPath.row * 2 + 10 + i;
        [cell.contentView addSubview:leftView];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapClick:)];
        [leftView addGestureRecognizer:tap];
    }
    cell.backgroundColor = [UIColor clearColor];
    return cell;
    
}
-(void)tapClick:(UITapGestureRecognizer *)sender
{
    CurrentDayItems *items = [_dataSource objectAtIndex:sender.view.tag - 10];
    
    gushixiangqingVC *gushi = [[gushixiangqingVC alloc] init];
    gushi.stop_id = items.spot_id;;
    gushi.photo = items.avatar_l;
    gushi.user_name = items.name;
    
    [self.navigationController pushViewController:gushi animated:YES];
   
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
